package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.MainViewAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.bean.ItemViewBean;
import com.bjesc_b2c.app.appraiset.bean.SaleCounBean;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AddCarSourceActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.AddDealerActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.DealerManagementActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.RepulseHistoryActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.SelectDealerActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.SourceEditActivity;
import com.bjesc_b2c.app.appraiset.mvp.view.activity.WaitCheckCarActivity;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import app.pinggushi.bjesc.com.baserecyclerview.adapter.RecycleViewDivider;
import butterknife.Bind;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

import static android.R.attr.version;


/**
 * Created by yidong on 2017/6/22.
 */

public class CollectionFragment extends BaseFragment implements MultiItemTypeAdapter.OnItemClickListener {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.iv_return)
    LinearLayout returnLin;
    private MainViewAdapter adapter;
    private List<ItemViewBean> list;
    private Intent mIntent;
    private String [] titles = {"巡店","创建经销商","经销商管理","巡店待采集","已上传车源","新增车源","打回历史"};// 去掉 车源编辑
    private int [] resID = {R.mipmap.patrol_shop,R.mipmap.create_dealer,R.mipmap.dealer_manage,R.mipmap.await_detect_car,R.mipmap.upload_car_source,R.mipmap.add_car_source,R.mipmap.car_source_ed,};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.layout_fragment, null);

        return super.onCreateView(inflater, container, savedInstanceState);
    }
    @Override
    public void initView() {
        returnLin.setVisibility(View.GONE);
        mIntent = new Intent();
        GridLayoutManager manager = new GridLayoutManager(getActivity(),3);
        recyclerView.setLayoutManager(manager);
        titleTv.setText(getResources().getText(R.string.collection));
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++){
            ItemViewBean bean = new ItemViewBean();
            bean.setTitle(titles[i]);
            bean.setResID(resID[i]);
            list.add(bean);
        }
        adapter = new MainViewAdapter(getActivity(),R.layout.item_gridview,list);
        RecycleViewDivider divider = new RecycleViewDivider(getActivity(), RecycleViewDivider.GRID,3, R.color.colorEBEBEB);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        recyclerView.addItemDecoration(divider);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getCollectCount();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        getCollectCount();
        Logger.d("onResume");
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        switch (position){
            case 0:
                mIntent.setClass(getActivity(),SelectDealerActivity.class);
//              mIntent.putExtra("TYPE",3);
                startIntent(mIntent);
                break;
            case 1:
                mIntent.setClass(getActivity(),AddDealerActivity.class);
                mIntent.putExtra("Title","创建经销商");
                startIntent(mIntent);
                break;
            case 2:
                mIntent.setClass(getActivity(),DealerManagementActivity.class);
                mIntent.putExtra("TYPE",4);
                startIntent(mIntent);
                break;
            case 3:
                mIntent.setClass(getActivity(),WaitCheckCarActivity.class);
                startIntent(mIntent);
                break;
            case 4:
                mIntent.clone();
                mIntent.setClass(getActivity(),SourceEditActivity.class);
                mIntent.putExtra("Title","已上传车源");
                mIntent.putExtra("TYPE","my_uploaded");
                startIntent(mIntent);
                break;
            case 5:
                mIntent.setClass(getActivity(),AddCarSourceActivity.class);
                startIntent(mIntent);
                break;
            case 6:
                mIntent.setClass(getActivity(),RepulseHistoryActivity.class);
                startIntent(mIntent);
//                mIntent.clone();
//                mIntent.setClass(getActivity(),SourceEditActivity.class);
//                mIntent.putExtra("Title","车源编辑");
//                mIntent.putExtra("TYPE","");
//                startIntent(mIntent);
                break;
        }

    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }



    private void getCollectCount() {
        HashMap<String, Object> map = new MapUtil()
                .put("version", version)
                .put("url", true).build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(getActivity(), map).create(IApiActivityRequest.class).getCollectCount(),
                new BaseSubscriber<TokenBean<SaleCounBean>>(getActivity()) {
                    @Override
                    public void onSuccess(TokenBean<SaleCounBean> o) {

                        if (TextUtils.isEmpty(o.getData().getInventory_count())) {
                            adapter.views.get(0).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(0).setVisibility(View.VISIBLE);
                            adapter.views.get(0).setText(o.getData().getInventory_count());
                        }
                        if (TextUtils.isEmpty(o.getData().getWait_check_count())) {
                            adapter.views.get(3).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(3).setVisibility(View.VISIBLE);
                            adapter.views.get(3).setText(o.getData().getWait_check_count());
                        }
                        if (TextUtils.isEmpty(o.getData().getExamine_no_count())) {
                            adapter.views.get(6).setVisibility(View.GONE);
                        } else {
                            adapter.views.get(6).setVisibility(View.VISIBLE);
                            adapter.views.get(6).setText(o.getData().getExamine_no_count());
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
